import { NodeSize } from '../config'

export default function stringOperations(flow) {
  flow.register('StringOperations', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getIconShape() {
        return h(
          'svg',
          {
            width: NodeSize,
            height: NodeSize,
            viewBox: '0 0 1024 1024'
          },
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M906.776381 816.615619a121.246476 121.246476 0 0 0-1.731048-27.721143l31.914667-23.356952-4.33981-11.312762a170.25219 170.25219 0 0 0-6.509714-14.457905l-5.558857-11.166476-37.888 8.679619a156.988952 156.988952 0 0 0-8.411429-9.606095 124.050286 124.050286 0 0 0-10.605714-10.313143l9.337905-37.912381-10.459429-5.900191a141.824 141.824 0 0 0-14.823619-7.338666l-11.410285-4.876191-23.186286 30.841905a121.807238 121.807238 0 0 0-27.233524-3.096381l-17.383619-35.059809-11.751619 2.169904c-5.315048 0.975238-10.678857 2.096762-15.847619 3.584l-11.922286 3.486477 1.633524 38.863238a123.928381 123.928381 0 0 0-22.576762 15.262476l-35.961905-15.701333-7.606857 9.337904a154.014476 154.014476 0 0 0-9.606095 13.263238l-6.729143 10.361905 26.307048 28.355048a121.465905 121.465905 0 0 0-7.753143 26.112l-37.766095 11.142095 0.121904 11.971048c0.048762 5.38819 0.390095 10.678857 0.999619 16.042666l1.414096 12.409905 38.814476 4.803048c2.706286 9.020952 6.339048 17.505524 10.898286 25.35619l-22.137905 32.865524 23.917714 25.770667 33.572572-15.750096 3.535238-2.169904c7.631238 4.973714 15.920762 9.094095 24.819809 12.361142l4.291048 39.302096 11.971047 1.901714c5.193143 0.804571 10.386286 1.243429 15.652572 1.536l12.458667 0.75581 11.312761-37.10781a106.203429 106.203429 0 0 0 26.721524-6.509714l28.720762 27.282285 10.337524-6.241523c4.583619-2.779429 9.045333-5.948952 13.433905-9.264762l9.971809-7.533715-15.238095-35.108571c6.144-6.607238 11.50781-13.945905 16.262095-22.211048l39.058286 2.438096 3.949714-11.361524a151.161905 151.161905 0 0 0 4.632381-15.725714l2.901334-12.214858-34.523429-17.237333z m-210.651429-5.315048a87.771429 87.771429 0 1 1 175.567238 0 87.771429 87.771429 0 1 1-175.567238 0zM712.240762 659.748571a100.303238 100.303238 0 0 1-11.166476 0.633905H601.965714v-130.608762h96.256c37.814857 0 55.003429 16.62781 55.003429 46.982096v40.131047c0 0.731429-0.146286 1.365333-0.170667 2.072381l49.054476-49.054476c-1.926095-37.302857-24.015238-58.148571-55.759238-63.609905 33.816381-12.629333 43.544381-34.377143 43.544381-63.585524v-44.129523c0-50.41981-29.793524-77.360762-89.941333-77.360762h-147.260952v381.586285h116.540952l43.008-43.056762zM601.965714 363.593143h95.085715c29.817905 0 43.568762 11.459048 43.568761 36.083809v42.983619c0 27.501714-14.336 44.690286-51.565714 44.690286H601.965714v-123.757714zM328.97219 321.194667L200.070095 702.780952h49.859048l30.939428-91.672381h162.133334l31.524571 91.672381h49.834667L395.459048 321.194667h-66.486858z m-34.962285 246.954666l68.193524-211.431619 68.193523 211.431619h-136.387047z'
            }
          )
        )
      }

      getLabelShape() {
        return h(
          'text',
          {
            fill: '#000000',
            fontSize: 12,
            x: NodeSize / 2,
            y: NodeSize + 20,
            textAnchor: 'middle',
            dominantBaseline: 'middle'
          },
          '字符串操作'
        )
      }

      getShape() {
        const { x, y, width, height } = this.getAttributes()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        return h(
          'g',
          {
            transform
          },
          [
            h(
              'rect',
              {
                rx: 5,
                ry: 5,
                width: NodeSize,
                height: NodeSize,
                fill: '#fff',
                stroke: '#3D6380',
                strokeWidth: 1
              }
            ),
            this.getIconShape(),
            this.getLabelShape()
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        super(data, graphModel)
        this.points = [
          [NodeSize / 2, 0],
          [NodeSize, NodeSize / 2],
          [NodeSize / 2, NodeSize],
          [0, NodeSize / 2]
        ]
      }
    }

    return {
      view: Node,
      model: Model
    }
  })
}
